package com.shkco.basic.util;

import com.shkco.basic.jwt.JwtUtils;
import com.shkco.basic.jwt.LoginData;
import com.shkco.basic.jwt.Payload;
import com.shkco.basic.jwt.RsaUtils;
import com.shkco.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            // 从请求头中获取token的值
            String token = request.getHeader("token");
            if (token != null){
                // 获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                // 使用公钥解密信息
                Payload<LoginData> infoFromToken = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                return infoFromToken.getLoginData().getLogininfo();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
